<?php
/*
 * Cookie操作类
 * @package    include
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 59debee41accecd166b7bb57522b7ba1b0e2f37a $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * Cookie常用操作
 * Class Cookie
 */
class Cookie {

	/** 权限 */
	const NAME_AUTH = 'auth';

	/** 分站 */
	const NAME_CITY      = 'city';
	const NAME_PHPSESSID = 'PHPSESSID';
	const NAME_USERNAME  = 'username';

	private static $_base64 = false;

	private static function _pre() {
		global $CFG;
		return $CFG['cookie_pre'];
	}

	/**
	 * 判断Cookie是否存在
	 * @param $name
	 * @return bool
	 */
	public static function has($name) {
		return isset($_COOKIE[self::_pre() . $name]);
	}

	/**
	 * 获取某个Cookie值
	 * @param $name
	 * @return string
	 */
	public static function get($name) {

		$value = isset($_COOKIE[self::_pre() . $name]) ? $_COOKIE[self::_pre() . $name] : '';
		if (self::$_base64) {
			$value = base64_decode(unserialize($value));

		}

		return $value;



	}

	// 设置某个Cookie值
	public static function set($name, $value, $expire = 0, $path = '', $domain = '') {
		global $CFG;
		if (empty($path)) {
			$path = $CFG['cookie_path'];
		}
		if (empty($domain)) {
			$domain = $CFG['cookie_domain'];
		}
		$expire = !empty($expire) ? time() + $expire : 0;
		if (self::$_base64) {
			$value = base64_encode(serialize($value));
		}
		return setcookie(self::_pre() . $name, $value, $expire, $path, $domain);
	}

	/**
	 * 删除某个Cookie值
	 * @param $name
	 */
	public static function remove($name) {
		self::set($name, '', time() - 3600);
		unset($_COOKIE[self::_pre() . $name]);
	}

	/**
	 * @param $name
	 * @see self::remove
	 * @deprecated
	 */
	public static function del($name) {
		self::remove($name);
	}

	/**
	 * 清空所有Cookie值
	 */
	public static function clear() {
		unset($_COOKIE);
	}
}